package view
{
	import com.greensock.TweenLite;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	/**
	 * 按钮逻辑分装 
	 * @author qidonghui
	 * 
	 */	
	public class ReaderButton extends Sprite
	{
		private var _skin:MovieClip;
		
		private var _icon:MovieClip;
		
		private var _bg:MovieClip;
		
		private var _style:Object;
		
		private var _enabled:Boolean = true;
		
		private var _selected:Boolean = false;
		
		public function ReaderButton(skin:MovieClip, style:Object)
		{
			this._skin = skin;
			_style = style;
			super();
			_icon = _skin["icon"];
			_bg = _skin["bg"];
			
			TweenLite.to(_icon, 0, {tint:_style.out});
			this.buttonMode = true;
			addChild(_skin);
			addListeners();	
		}
		
		private function addListeners():void
		{
			this.addEventListener(MouseEvent.MOUSE_OVER, overThis);
			this.addEventListener(MouseEvent.ROLL_OUT, outThis);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}
		
		private function removeListeners():void
		{
			this.removeEventListener(MouseEvent.MOUSE_OVER, overThis);
			this.removeEventListener(MouseEvent.ROLL_OUT, outThis);
			this.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}
		
		protected function onMouseDown(event:MouseEvent):void
		{
			TweenLite.to(_icon, 0, {tint:_style.down});
			_bg.gotoAndStop(2);
			stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
		}
		
		protected function onStageMouseUp(event:MouseEvent):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);	
			if(this.hitTestPoint(stage.mouseX, stage.mouseY))
			{
				TweenLite.to(_icon, 0, {tint:_style.over});
			}else{
				TweenLite.to(_icon, 0, {tint:_style.out});
			}
			_bg.gotoAndStop(1);
		}
		
		protected function outThis(event:MouseEvent):void
		{
			TweenLite.to(_icon, 0.1, {tint:_style.out});
			_bg.gotoAndStop(1);
		}
		
		protected function overThis(event:MouseEvent):void
		{
			TweenLite.to(_icon, 0.1, {tint:_style.over});
			_bg.gotoAndStop(1);
		}
		
		public function get enabled():Boolean { return _enabled; }
		public function set enabled(value:Boolean):void
		{
			if(value == _enabled) return;
			_enabled = value;
			if(_enabled)
			{
				addListeners();
				if(this.hitTestPoint(stage.mouseX, stage.mouseY))
				{
					TweenLite.to(_icon, 0, {tint:_style.over});
				}else{
					TweenLite.to(_icon, 0, {tint:_style.out});
				}
				this.mouseEnabled = this.mouseChildren = true;
				this.buttonMode = true;
				
			}else{
				this.mouseEnabled = this.mouseChildren = false;
				removeListeners();
				TweenLite.to(_icon, 0, {tint:_style.disable});
				this.buttonMode = false;
			}
		}

		public function get selected():Boolean { return _selected; }
		public function set selected(value:Boolean):void
		{
			if(value == _selected) return;
			_selected = value;
			if(_selected)
			{
				removeListeners();
				TweenLite.to(_icon, 0, {tint:_style.down});
				_bg.gotoAndStop(2);
				this.buttonMode = false;
			}else{
				addListeners();
				if(this.hitTestPoint(stage.mouseX, stage.mouseY))
				{
					TweenLite.to(_icon, 0, {tint:_style.over});
				}else{
					TweenLite.to(_icon, 0, {tint:_style.out});
				}
				_bg.gotoAndStop(1);
				this.buttonMode = true;
			}
		}
		
	}
	
}